package com.gordon.tips.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.gordon.tips.domain.Topic;
import com.gordon.tips.log.Logger;
import com.gordon.tips.log.LoggerImpl;
import com.gordon.tips.service.TopicService;
import com.gordon.tips.util.Page;

/**
 * Servlet Filter implementation class ForumFilter
 */
public class ForumFilter implements Filter {
	
	private static Logger logger = new LoggerImpl(ForumFilter.class);
	private TopicService topicService;

    /**
     * Default constructor. 
     */
    public ForumFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		List<Topic>topics=topicService.getTopic(0, 10);
		req.setAttribute("topics", topics);
		int number_topics=topicService.countAllTopics();
		Page page=new Page(number_topics,10,"0");
		req.setAttribute("page", page);
		logger.debug("ForumFilter is working.");
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

	//getter and setter
	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}
	
	

}
